package com.hqyj.freemovie.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.freemovie.front.domain.Movie;
import com.hqyj.freemovie.front.json.R;
import com.hqyj.freemovie.front.service.MovieService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("movie")
public class MovieController extends BaseController {

    @Resource
    private MovieService movieService;

    @SaCheckLogin
    @PostMapping("/")
    public R saveMovie(@RequestBody Movie movie) {
        System.out.println("-----------要新增的电子资料----------");
        System.out.println(movie);

        try {
            movieService.saveMovie(movie);
            return R.success("新增电影资料成功！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }


    @SaCheckLogin
    @PutMapping("/")
    public R updateMovie(@RequestBody Movie movie) {
        System.out.println("-----------要更新的电子资料----------");
        System.out.println(movie);

        try {
            movieService.updateMovie(movie);
            return R.success("更新电影资料成功！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }


    @GetMapping("/catalog/pager/{type}")
    public R queryMoviePagerByType(@PathVariable("type") String type, @RequestParam("keyword") String keyword ,@RequestParam("currentPageNumber") Integer currentPageNumber) {
        try {
            Page<Movie> page = movieService.queryMoviePagerByType(type,keyword, currentPageNumber);
            return R.success("查询指定板块分页电影列表成功！", page);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/query/{id}")
    public R queryMovieById(@PathVariable("id") Integer id){
        try{

            Movie movie = movieService.queryMovieById(id);
            return R.success("查询电影资料成功！",movie);
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @SaCheckLogin
    @SaCheckPermission(value = {"movie.query"}, mode = SaMode.OR)
    @GetMapping("test")
    public R testMovie(){
        System.out.println("movie test.....");
        return R.success("OK");
    }
}
